package com.fish.anne.service.impl;

import java.util.List;

import com.fish.anne.dao.MemberDao;
import com.fish.anne.dao.ibatis.MemberDaoiBatis;
import com.fish.anne.model.Member;
import com.fish.anne.service.MemberManager;
import com.fish.anne.webapp.formbean.MemberSearch;

 public class MemberManagerImpl extends GenericManagerImpl<Member, Long> implements MemberManager {
    MemberDao memberDao = new MemberDaoiBatis();
   
    
    
    public MemberManagerImpl(MemberDao memberDao) {
        super(memberDao);
        this.memberDao = memberDao;
    }

	public String saveMember(Member member) {
		if(this.haveMember(member)){
			return "该用户名已经被注册";
		}else{
			this.memberDao.save(member);
			return "注册成功";
		}
	}
	
	private boolean haveMember(Member member){
		Member memberDB = this.memberDao.getMemberByUserName(member.getEmail());
		if(memberDB!=null){
			return true;
		}else{
			return false;
		}
	}
	
	Long memberId = 0l;
	
	public Member login(Member member){
		Member memberDB = this.memberDao.getMemberByUserName(member.getUname());
		if(memberDB!=null&&memberDB.getPassword().equals(member.getPassword())){
			 
			return memberDB;
		}else{
			return null;
		}
	}
	
 
	
	public String changePassword(String userId,String oldPass,String newPass){
		Member member = new Member();
		member = memberDao.get(Long.parseLong(userId));
		if(member.getPassword().equals(oldPass)){
			member.setPassword(newPass);
			memberDao.save(member);
			return "密码修改成功";
		}else{
			return "原始密码错误";
		}
		
	}
	
	public Long getCountOfSearch(MemberSearch memberSearch){
		return this.memberDao.getCountOfSearch(memberSearch);
	}
	public List<Member> findBySearch(MemberSearch memberSearch){
		return  this.memberDao.findBySearch(memberSearch);
	}
	
}